package com.bsoft.gol.hcb.dao.evaluation;

import com.bsoft.gol.hcb.utils.DateUtils;
import ctd.account.UserRoleToken;
import ctd.controller.exception.ControllerException;
import ctd.persistence.DAOFactory;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessAction;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import hcn.base.evaluation.EvaluationItemRecord;
import hcn.base.evaluation.EvaluationRecord;
import hcn.common.CodeDefinition;
import hcn.util.RpcUserToken;
import org.apache.commons.lang3.StringUtils;
import org.atag.dao.util.CustomAliasToEntityMapResultTransformer;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import pcn.vo.evaluation.GetDetailByIdVo;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by mlm on 2015/11/30.
 * <p/>
 * 满意度评价记录
 */
@SsdevDao(serviceId = "evaluationRecordDAOService")
public abstract class EvaluationRecordDAO extends HibernateSupportDelegateDAO<EvaluationRecord> {

    public EvaluationRecordDAO() {
        this.setKeyField("id");
    }

    /**
     * 查看评价列表
     *
     * @param firstUserId
     * @return
     */
    @RpcService
    public List<Object[]> findEvaluateListByUserId(final String firstUserId) {
        HibernateStatelessResultAction<List<Object[]>> action = new AbstractHibernateStatelessResultAction<List<Object[]>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sql = new StringBuilder("select s.org_name, s.doct_name,s.work_date, ");
                sql.append(" s.id,s.level,def.business_name,s.first_flag,s.add_flag,s.org_id,s.business_id ");
                sql.append(" from ods_evaluationrecord s  ");
                sql.append(" left join base_evaluationdefine def on s.business_type=def.business_type ");
                sql.append(" where s.first_user_id=:firstUserId  and def.occasion='2' ");
                sql.append(" and s.tenant_id=? ");
                sql.append(" order by s.id desc");
                SQLQuery sqlQuery = ss.createSQLQuery(sql.toString());
                sqlQuery.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                sqlQuery.setString(0, UserRoleToken.getCurrentTenantId());
                sqlQuery.setString("firstUserId", firstUserId);
                List<Object[]> dbList = sqlQuery.list();
                setResult(dbList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 查看评价列表(根据医生姓名查找)
     *
     * @param firstUserId
     * @param doctName
     * @return
     */
    @RpcService
    public List<Object[]> findEvaluateListByUserIdAndDoctName(final String firstUserId, final String doctName) {
        HibernateStatelessResultAction<List<Object[]>> action = new AbstractHibernateStatelessResultAction<List<Object[]>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sql = new StringBuilder("select s.org_name, s.doct_name,s.work_date, ");
                sql.append(" s.id,s.level,def.business_name,s.first_flag,s.add_flag,s.org_id,s.business_id ");
                sql.append(" from ods_evaluationrecord s  ");//left join ods_regprepare r on s.business_id=r.id
                sql.append(" left join base_evaluationdefine def on s.business_type=def.business_type ");
//                sql.append(" where s.first_user_id='"+firstUserId+"' and dct.name like '%"+doctName+"%' order by s.id desc ");
                sql.append(" where s.first_user_id=:firstUserId and doct_name like :doctName and def.occasion='2'");
                sql.append(" and s.tenant_id = ? ");
                sql.append(" order by s.id desc");
                SQLQuery sqlQuery = ss.createSQLQuery(sql.toString());
                sqlQuery.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                sqlQuery.setParameter("firstUserId", firstUserId);
                sqlQuery.setParameter("doctName", "%" + doctName + "%");
                sqlQuery.setString(0, UserRoleToken.getCurrentTenantId());
                List<Object[]> dbList = sqlQuery.list();
                setResult(dbList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 查看详情
     *
     * @param id
     * @return
     */
    @RpcService
    public GetDetailByIdVo getDetailById(final int id) {
        HibernateStatelessResultAction<GetDetailByIdVo> action = new AbstractHibernateStatelessResultAction<GetDetailByIdVo>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sql = new StringBuilder("select doct_name,level,doctor_id,s.work_date, ");
                sql.append(" s.first_content,s.add_content,s.id,s.org_id,s.reg_dept_id,s.sex,s.reg_dept_name ");
                sql.append(" from ods_evaluationrecord s ");
                sql.append(" where s.id=:id");
                SQLQuery sqlQuery = ss.createSQLQuery(sql.toString());
                sqlQuery.setParameter("id", id);
                sqlQuery.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                Map o = (Map) sqlQuery.uniqueResult();
                if (o != null) {
                    GetDetailByIdVo convert = ConversionUtils.convert(o, GetDetailByIdVo.class);
                    setResult(convert);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 医生被评价列表详情(所有)
     *
     * @param doctorId
     * @param start
     * @param limit
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select s.id,u.name,s.firstContent,s.addContent,s.firstDt,s.addDt,def.businessName " +
            "from EvaluationRecord s,EvaluationDefine def,User u " +
            "where s.doctorId=:doctorId and s.firstUserId=u.id " +
            "and s.businessType=def.businessType and s.tenantId =:tenantId)"
            , params = {@DAOParam(name = "tenantId", value = "%$ur.tenantId", dataType = "string")})
    public abstract List<Object[]> findAllListFromDoct(@DAOParam("doctorId") String doctorId, @DAOParam(pageStart = true) int start, @DAOParam(pageLimit = true) int limit);


    /**
     * 医生被评价列表详情(查询条件:businessType)
     *
     * @param doctorId
     * @param start
     * @param limit
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select s.id,u.name,s.firstContent,s.addContent,s.firstDt,s.addDt,def.businessName " +
            "from EvaluationRecord s,EvaluationDefine def,User u " +
            "where s.doctorId=:doctorId and s.firstUserId=u.id " +
            "and s.businessType=def.businessType and s.businessType=:businessType"
            + " and s.tenantId =:tenantId)"
            , params = {@DAOParam(name = "tenantId", value = "%$ur.tenantId", dataType = "string")})
    public abstract List<Object[]> findListFromDoctByBusinessType(@DAOParam("doctorId") String doctorId, @DAOParam("businessType") String businessType, @DAOParam(pageStart = true) int start, @DAOParam(pageLimit = true) int limit);

    /**
     * 医生被评价列表详情(查询条件:有文字,businessType)
     *
     * @param doctorId
     * @param start
     * @param limit
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select s.id,u.name,s.firstContent,s.addContent,s.firstDt,s.addDt,def.businessName " +
            "from EvaluationRecord s,EvaluationDefine def,User u " +
            "where s.doctorId=:doctorId and s.firstUserId=u.id " +
            "and s.businessType=def.businessType and (s.firstContent is not null or s.addContent is not null) and s.businessType=:businessType "
            + "and s.tenantId=:tenantId)"
            , params = {@DAOParam(name = "tenantId", value = "%$ur.tenantId", dataType = "string")})
    public abstract List<Object[]> findListFromDoctByWordAndBusinessType(@DAOParam("doctorId") String doctorId, @DAOParam("businessType") String businessType, @DAOParam(pageStart = true) int start, @DAOParam(pageLimit = true) int limit);

    /**
     * 医生被评价列表详情(查询条件:有文字)
     *
     * @param doctorId
     * @param start
     * @param limit
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select s.id,u.name,s.firstContent,s.addContent,s.firstDt,s.addDt,def.businessName " +
            "from EvaluationRecord s,EvaluationDefine def,User u " +
            "where s.doctorId=:doctorId and s.firstUserId=u.id " +
            "and s.businessType=def.businessType and (s.firstContent is not null or s.addContent is not null) "
            + "and s.tenantId=:tenantId)"
            , params = {@DAOParam(name = "tenantId", value = "%$ur.tenantId", dataType = "string")})
    public abstract List<Object[]> findListFromDoctByWord(@DAOParam("doctorId") String doctorId, @DAOParam(pageStart = true) int start, @DAOParam(pageLimit = true) int limit);


    /**
     * 医生被评价列表详情(查询条件:有追评,businessType)
     *
     * @param doctorId
     * @param start
     * @param limit
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select s.id,u.name,s.firstContent,s.addContent,s.firstDt,s.addDt,def.businessName " +
            "from EvaluationRecord s,EvaluationDefine def,User u " +
            "where s.doctorId=:doctorId and s.firstUserId=u.id " +
            "and s.businessType=def.businessType and s.addFlag=true and s.businessType=:businessType "
            + "and s.tenantId=:tenantId)"
            , params = {@DAOParam(name = "tenantId", value = "%$ur.tenantId", dataType = "string")})
    public abstract List<Object[]> findListFromDoctHasAddEvaluationAndBusinessType(@DAOParam("doctorId") String doctorId, @DAOParam("businessType") String businessType, @DAOParam(pageStart = true) int start, @DAOParam(pageLimit = true) int limit);

    /**
     * 医生被评价列表详情(查询条件:有追评)
     *
     * @param doctorId
     * @param start
     * @param limit
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select s.id,u.name,s.firstContent,s.addContent,s.firstDt,s.addDt,def.businessName " +
            "from EvaluationRecord s,EvaluationDefine def,User u " +
            "where s.doctorId=:doctorId and s.firstUserId=u.id " +
            "and s.businessType=def.businessType and s.addFlag=true "
            + "and s.tenantId=:tenantId)"
            , params = {@DAOParam(name = "tenantId", value = "%$ur.tenantId", dataType = "string")})
    public abstract List<Object[]> findListFromDoctHasAddEvaluation(@DAOParam("doctorId") String doctorId, @DAOParam(pageStart = true) int start, @DAOParam(pageLimit = true) int limit);


    /**
     * 医生被评价列表详情(查询条件:花钱少,有爱心，多临床，疗效好满分,businessType)
     *
     * @param doctorId
     * @param start
     * @param limit
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select s.id,u.name,s.firstContent,s.addContent,s.firstDt,s.addDt,def.businessName " +
            "from EvaluationRecord s,EvaluationDefine def,User u,EvaluationItem item,EvaluationItemRecord ir " +
            "where s.doctorId=:doctorId and s.firstUserId=u.id " +
            "and s.businessType=def.businessType and ir.recordId=s.id and ir.itemId=item.id " +
            "and item.content like :itemName and ir.level=5 and s.businessType=:businessType "
            + "and s.tenantId=:tenantId)"
            , params = {@DAOParam(name = "tenantId", value = "%$ur.tenantId", dataType = "string")})
    public abstract List<Object[]> findListFromDoctByItemAndBusinessType(@DAOParam("doctorId") String doctorId, @DAOParam("itemName") String itemName, @DAOParam("businessType") String businessType, @DAOParam(pageStart = true) int start, @DAOParam(pageLimit = true) int limit);

    /**
     * 医生被评价列表详情(查询条件:花钱少,有爱心，多临床，疗效好满分)
     *
     * @param doctorId
     * @param start
     * @param limit
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select s.id,u.name,s.firstContent,s.addContent,s.firstDt,s.addDt,def.businessName " +
            "from EvaluationRecord s,EvaluationDefine def,User u,EvaluationItem item,EvaluationItemRecord ir " +
            "where s.doctorId=:doctorId and s.firstUserId=u.id " +
            "and s.businessType=def.businessType and ir.recordId=s.id and ir.itemId=item.id " +
            "and item.content like :itemName and ir.level=5 "
            + "and s.tenantId=:tenantId)"
            , params = {@DAOParam(name = "tenantId", value = "%$ur.tenantId", dataType = "string")})
    public abstract List<Object[]> findListFromDoctByItem(@DAOParam("doctorId") String doctorId, @DAOParam("itemName") String itemName, @DAOParam(pageStart = true) int start, @DAOParam(pageLimit = true) int limit);


    /**
     * 医生被评价列表详情(查询条件:差评,businessType)
     *
     * @param doctorId
     * @param start
     * @param limit
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select s.id,u.name,s.firstContent,s.addContent,s.firstDt,s.addDt,def.businessName " +
            "from EvaluationRecord s,EvaluationDefine def,User u,EvaluationItemRecord ir " +
            "where s.doctorId=:doctorId and s.firstUserId=u.id " +
            "and s.businessType=def.businessType and ir.recordId=s.id and ir.level<=:level and s.businessType=:businessType "
            + "and s.tenantId=:tenantId)"
            , params = {@DAOParam(name = "tenantId", value = "%$ur.tenantId", dataType = "string")})
    public abstract List<Object[]> findListFromDoctByLevelAndBusinessType(@DAOParam("doctorId") String doctorId, @DAOParam("level") int level, @DAOParam("businessType") String businessType, @DAOParam(pageStart = true) int start, @DAOParam(pageLimit = true) int limit);

    /**
     * 医生被评价列表详情(查询条件:差评)
     *
     * @param doctorId
     * @param start
     * @param limit
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select s.id,u.name,s.firstContent,s.addContent,s.firstDt,s.addDt,def.businessName " +
            "from EvaluationRecord s,EvaluationDefine def,User u,EvaluationItemRecord ir " +
            "where s.doctorId=:doctorId and s.firstUserId=u.id " +
            "and s.businessType=def.businessType and ir.recordId=s.id and ir.level<=:level "
            + "and s.tenantId=:tenantId)"
            , params = {@DAOParam(name = "tenantId", value = "%$ur.tenantId", dataType = "string")})
    public abstract List<Object[]> findListFromDoctByLevel(@DAOParam("doctorId") String doctorId, @DAOParam("level") int level, @DAOParam(pageStart = true) int start, @DAOParam(pageLimit = true) int limit);


    /**
     * 通过业务id查询记录
     *
     * @param businessId
     * @return
     */
    @RpcService
    @DAOMethod(sql = "from EvaluationRecord s where s.businessId=:businessId ")
    public abstract List<EvaluationRecord> findByBusinessId(@DAOParam("businessId") int businessId);

    /**
     * 通过业务id和业务类型查询记录
     *
     * @param businessId
     * @param businessType
     * @return
     */
    @RpcService
    @DAOMethod(sql = "from EvaluationRecord s where s.businessType=:businessType and s.businessId=:businessId ")
    public abstract List<EvaluationRecord> findByBusinessIdAndType(@DAOParam("businessType") String businessType,@DAOParam("businessId") int businessId);


    /**
     * 保存满意度评价记录和评价项目记录集合
     *
     * @param record
     * @param itemList
     */
    @RpcService
    public void saveRecord(final EvaluationRecord record, final List<Map<String, Integer>> itemList, final Date date) {
        HibernateSessionTemplate.instance().executeTrans(new HibernateStatelessAction() {

            @Override
            public void execute(StatelessSession ss) throws Exception {
                String doctorId = null;
                String userId = null;
                String orgId = null;
                Integer dbRecordId = null;
                if (record.getId() != 0) {   //更新
                    EvaluationRecord dbRecord = (EvaluationRecord) ss.get(EvaluationRecord.class, record.getId());
                    if (dbRecord == null) {
                        throw new ControllerException(CodeDefinition.SATISFACTION_EVALUATE, " no record found with id " + record.getId());
                    }
                    dbRecordId = dbRecord.getId();
                    doctorId = dbRecord.getDoctorId();
                    userId = record.getAddUserId();
                    orgId = dbRecord.getOrgId();

                    if (StringUtils.isNotBlank(record.getAddContent())) {
                        dbRecord.setAddContent(record.getAddContent());
                    }
                    dbRecord.setAddUserId(record.getAddUserId());
                    dbRecord.setAddDt(date);
                    dbRecord.setAddFlag(true);
                    ss.update(dbRecord);
                } else {
                    doctorId = record.getDoctorId();
                    userId = record.getFirstUserId();
                    orgId = record.getOrgId();
                    dbRecordId = (Integer) ss.insert(record);
                }
                if (itemList != null && itemList.size() > 0) {
                    EvaluationItemRecordDAO itemRecordDAO = DAOFactory.getDAO(EvaluationItemRecordDAO.class);
                    for (Map<String, Integer> item : itemList) {
                        Integer id = item.get("itemId");
                        Integer level = item.get("level");
                        EvaluationItemRecord itemEntity = new EvaluationItemRecord();
                        itemEntity.setItemId(id);
                        itemEntity.setLevel(level);
                        itemEntity.setEvaluateDt(date);
                        itemEntity.setRecordId(dbRecordId);
                        itemRecordDAO.save(itemEntity);
                    }
                }
            }
        });
    }

    /**
     * getEvaluationById 获得评价平均值
     *
     * @param doctorId
     * @return
     * @throws
     * @author chase
     * @date 2016/4/7
     */
    @RpcService
    public List<Map<String, Object>> getEvaluationAvgById(final String doctorId, final String orgId) {
        HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                String hsql = "select avg(ir.level) as avglevel, s.doctor_id  from  ods_evaluationrecord s,  ods_evaluationitemrecord ir " +
                        " where  ir.record_id=s.id and s.doctor_id=:doctorId and   s.org_id =:orgId  group by s.doctor_id  ";
                SQLQuery query = ss.createSQLQuery(hsql);
                query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                query.setParameter("doctorId", doctorId);
                query.setParameter("orgId", orgId);
                List<Map<String, Object>> ls = (List<Map<String, Object>>) query.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * getEvaluationById 获得评价平均值
     *
     * @param doctorId
     * @return
     * @throws
     * @author chase
     * @date 2016/4/7
     */
    @RpcService
    public List<Map<String, Object>> getEvaluationAvgById(final String doctorId, final String orgId, final int month) {
        HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                String hsql = "select avg(ir.level) as avglevel, s.doctor_id  from  ods_evaluationrecord s,  ods_evaluationitemrecord ir " +
                        " where  ir.record_id=s.id and s.doctor_id= ? and   s.org_id =? ";
                if (month != -1) {
                    hsql += " and s.add_dt>DATE_SUB(CURDATE(), INTERVAL ? MONTH) ";
                }
                hsql += "group by s.doctor_id  ";
                SQLQuery query = ss.createSQLQuery(hsql);
                query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                query.setParameter(0, doctorId);
                query.setParameter(1, orgId);
                if (month != -1) {
                    query.setParameter(2, month);
                }
                List<Map<String, Object>> ls = (List<Map<String, Object>>) query.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * getEvaluationById 获得评价平均值
     *
     * @param doctorId
     * @return
     * @throws
     * @author chase
     * @date 2016/4/7
     */
    @RpcService
    public List<Map<String, Object>> getEvaluationAvgById(final String doctorId, final int month) {
        HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                String hsql = "select avg(ir.level) as avglevel, s.doctor_id  from  ods_evaluationrecord s,  ods_evaluationitemrecord ir " +
                        " where  ir.record_id=s.id and s.doctor_id=:doctorId ";
                if (month != -1) {
                    hsql += " and s.add_dt>:addDt ";
                }
                hsql += " group by s.doctor_id  ";
                SQLQuery query = ss.createSQLQuery(hsql);
                query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                query.setParameter("doctorId", doctorId);
                if (month != -1) {
                    query.setParameter("addDt", DateUtils.calculateTime(new Date(), month));
                }
                List<Map<String, Object>> ls = (List<Map<String, Object>>) query.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * getEvaluationCountById
     *
     * @param
     * @return
     * @throws
     * @Description:统计评价次数
     * @author chase
     * @date 2016/7/4
     */
    @RpcService
    public List<Map<String, Object>> getEvaluationCountById(final String doctorId, final int month) {
        HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                String hsql = "select sum(1) as num, s.doctor_id  from ods_evaluationrecord s,ods_evaluationitemrecord ir " +
                        "where  ir.record_id=s.id and s.doctor_id= ? ";
                if (month != -1) {
                    hsql += " and s.add_dt>DATE_SUB(CURDATE(), INTERVAL ? MONTH) ";
                }
                hsql += "group by s.doctor_id  ";
                SQLQuery query = ss.createSQLQuery(hsql);
                query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                query.setParameter(0, doctorId);
                if (month != -1) {
                    query.setParameter(1, month);
                }
                List<Map<String, Object>> ls = (List<Map<String, Object>>) query.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    @RpcService
    public List<Map<String, Object>> getEvaluationList(final String doctorId, final int page, final int limit) {
        HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("select a.*,b.name from ods_evaluationrecord a,base_user b where a.first_user_id=b.id and a.doctor_id=:doctorId ");
                hsql.append(" and a.org_id in (select org_id from base_orgcloudmem where cloud_id=:tenantId)");
                Query query = ss.createSQLQuery(hsql.toString());
                query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                query.setParameter("doctorId", doctorId);
                String tenantId = UserRoleToken.getCurrentTenantId();
                if (tenantId == null || tenantId.isEmpty()) {
                    tenantId = RpcUserToken.getTenantId();
                }
                query.setParameter("tenantId", tenantId);
                query.setFirstResult((page - 1) * limit);
                query.setMaxResults(limit);
                List<Map<String, Object>> ls = (List<Map<String, Object>>) query.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    @RpcService
    public List<Map<String, Object>> getEvaluationById(final int id) {
        HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("select a.*,b.name from ods_evaluationrecord a,base_user b where a.first_user_id=b.id and a.id=:id ");
                Query query = ss.createSQLQuery(hsql.toString());
                query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                query.setParameter("id", id);
                List<Map<String, Object>> ls = (List<Map<String, Object>>) query.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    @RpcService
    public List<Map<String, Object>> getEvaluationByDefineId(final int id, final int defineId) {
        HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append(" select avg(b.level) as avglevel ,c.content,c.id ");
                hsql.append(" from  ods_evaluationitemrecord b ,base_evaluationitem c ");
                hsql.append(" where b.record_id=:recordId and c.define_id=:defineId ");
                hsql.append(" group by c.content,c.id ");
                Query query = ss.createSQLQuery(hsql.toString());
                query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                query.setParameter("recordId", id);
                query.setParameter("defineId", defineId);
                List<Map<String, Object>> ls = (List<Map<String, Object>>) query.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}
